﻿#ifndef BUSINESSFILE_H
#define BUSINESSFILE_H

#include <QJsonObject>
#include <map>

#include <protocol/protocol.h>
#include <hv/Channel.h>
#include <nlohmann/json.hpp>
#include "businessuser.h"

class BusinessFile
{
public:
    // 删除拷贝构造函数和拷贝赋值操作符
    BusinessFile(BusinessFile const&) = delete;
    BusinessFile& operator=(BusinessFile const&) = delete;
    // 提供一个全局唯一的接口
    static BusinessFile& getInstance() {
        static BusinessFile instance;
        return instance;
    }
    // upload file
    void uploadFile(QJsonObject& in, QJsonObject& out);
    // download file
    void downloadFile(QJsonObject& in, QJsonObject& out);


    // void add File
    void addFileNameAndID(const std::string& name, uint64_t id);


    // receive a request of uploading file
    void recvUploadFileRequest(std::shared_ptr<Protocol> protocol);
    // receive a file block transmission
    void recvFileBlock(std::shared_ptr<Protocol> protocol);
    // transmiss end
    void recvEndFileTransmission(std::shared_ptr<Protocol> protocol);

    // request refresh file list
    void refreshFileList(QJsonObject& in, QJsonObject& out);

    // 注册一个图片传输
    void registerPictureTrans(uint64_t fileName, uint64_t syncPoint);
    // 释放一个图片传输（如果目标传输id不是传输图片的，则无效果）
    void tryReleasePictureTrans(uint64_t fileName);

private:
    BusinessFile();

    uint64_t str2ULL(const std::string& str);

    std::map<std::string, uint64_t> FileName2ID;
    std::map<uint64_t, std::pair<std::chrono::high_resolution_clock::time_point, std::chrono::high_resolution_clock::time_point>> ID2Time;
    std::map<uint64_t, nlohmann::json> FileInfo;

    std::map<uint64_t, uint64_t> picSyncStore;

};

#endif // BUSINESSFILE_H
